package cn.com.nes.site.module.service;

import cn.com.nes.common.util.DBUtils;
import cn.com.nes.site.entity.DataReq;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.module.dao.AdDaoImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Connection;
import java.sql.SQLException;

@Service
public class AdService {

	@Resource
	private AdDaoImpl impl;

	//热链云获取广告列表
	public PageData getAdList(DataReq req) throws SQLException {
		PageData res = new PageData();
		Connection conn = null;
		try {
			conn = DBUtils.getJNDIDBConn();
			res = impl.getAdList(conn,req);
		} catch (Exception e) {
			e.printStackTrace();
			conn.rollback();
		} finally {
			if (conn != null)
				conn.close();
		}
		return res;
	}

	//热链云保存广告信息
	public PageData saveAd(DataReq req) throws SQLException {
		PageData res = new PageData();
		Connection conn = null;
		try {
			conn = DBUtils.getJNDIDBConn();
			res = impl.saveAd(conn,req);
			conn.commit();
		} catch (Exception e) {
			e.printStackTrace();
			conn.rollback();
		} finally {
			if (conn != null)
				conn.close();
		}
		return res;
	}

	//热链云删除广告信息
	public PageData delAd(DataReq req) throws SQLException {
		PageData res = new PageData();
		Connection conn = null;
		try {
			conn = DBUtils.getJNDIDBConn();
			res = impl.delAd(conn,req);
			conn.commit();
		} catch (Exception e) {
			e.printStackTrace();
			conn.rollback();
		} finally {
			if (conn != null)
				conn.close();
		}
		return res;
	}

	//小程序获取对应广告
	public PageData getAd(DataReq req) throws SQLException {
		PageData res = new PageData();
		Connection conn = null;
		try {
			conn = DBUtils.getJNDIDBConn();
			res = impl.getAd(conn,req);
		} catch (Exception e) {
			e.printStackTrace();
			conn.rollback();
		} finally {
			if (conn != null)
				conn.close();
		}
		return res;
	}

}